﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using AdaptiveHelpDesk;

namespace Novice
{
    /// <summary>
    /// The role of this service is to process requests from services for inputing data from the generated user interface.
    /// </summary>
    [WebService(Namespace = "http://soaiui.nmmu.ac.za/")]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ProcessingService : System.Web.Services.WebService
    {
        
        StoredProcedures sp = new StoredProcedures();
        GetTaskModel gt = new GetTaskModel();
        ArrayList userDetails = new ArrayList();

        String TodayDate; //DateTime.Today.ToShortDateString().Replace('/', '-');
        String TodayTime;//DateTime.Now.ToLongTimeString();

        [WebMethod]
        public void processStep(int step, DataObject[] idx)
        {
            TodayDate = gt.GetLogginDate();  
            TodayTime = gt.GetLogginTime();  

            switch (step)
            {
                default:
                    break;
                case 1:
                    Step1(idx);
                    break;
                case 2:
                    Step2(idx);
                    break;
                case 3:
                    Step3(idx);
                    break;
                case 4:
                    Step4(idx);
                    break;
                case 5:
                    processAll(idx);
                    break;
            }
        }

        [WebMethod]
        public string Step1(DataObject[] idx)
        {
            String username = gt.CheckCurrentUser();
            userDetails = (ArrayList)sp.getPassword(username);
            if (userDetails.Count != 0)
            {
                String userid = userDetails[1].ToString();
                String fname = userDetails[2].ToString();
                String lname = userDetails[3].ToString();
                String stereotype = userDetails[4].ToString();
                String CallID = getCallID();
                
                gt.SetCallID(CallID);
                
                // Get Current Customer ID from working data
                String CustomerID = gt.GetSelectedCustomer();
            }

            return "success";
        }
        [WebMethod]
        public string Step2(DataObject[] idx)
        {
            /* NB NB
             * idx contains the data from the UI. the name of each Object must match the variable to the Stored Procedure Call
             * e.g. Loop through list, and find  the names for the appropriate parameter for the Stored Procedure Method, and store that 
             * in a local variable
             * 
             *  String custId;
             *  for(int i = 0; i < idx.length; i++){
             *      if(idx[i].name == "customerID")
             *          DataObject cur = idx[i];
             *  }
             * 
             **/
            String username = gt.CheckCurrentUser();
            userDetails = (ArrayList)sp.getPassword(username);
            if (userDetails.Count != 0)
            {
                String userid = userDetails[1].ToString();
                //NB NB NB
                // Get the next call ID  TODO: Persist this in such a way that step 2 and 3 have access to it...maybe in Task Model?
                String CallID = gt.GetCallID();
                // Get Current Customer ID from Task model  or wherever i store temporary data
                String CustomerID = gt.GetSelectedCustomer();
                String ServiceData = getData("serviceName", idx);
                String CallTypeData = getData("callType", idx);
                String SubCallData = getData("subCallType", idx);
                String SeverityData = getData("severity", idx);
                String SourceData = getData("source", idx);
                String CallDescription = getData("Description", idx);
                sp.LogACall(CallID, TodayDate, TodayTime, userid, CustomerID, ServiceData, CallTypeData, SubCallData, "Open", SeverityData, SourceData, CallDescription);
            }
            return "success";
        }
        [WebMethod]
        public string Step3(DataObject[] idx)
        {
            String username = gt.CheckCurrentUser();
            userDetails = (ArrayList)sp.getPassword(username);
            if (userDetails.Count != 0)
            {
                // Get this CallID from the persisted storage spot specified in step 2
                String CallID = gt.GetCallID(); ;
                String AssigneeGroupData = getData("Group", idx);
                String AssigneeContactData = getData("Contact", idx);
                String AssigneeFName = getData("Firstname", idx);
               //Assign the call
                sp.AssignACall(CallID, AssigneeContactData, AssigneeGroupData, AssigneeFName, TodayDate, TodayTime, "");
            }
            return "success";
        }
        [WebMethod]
        public string Step4(DataObject[] idx)
        {
            String username = gt.CheckCurrentUser();
            userDetails = (ArrayList)sp.getPassword(username);
            if (userDetails.Count != 0)
            {
                // TODO: Get this CallID from the persisted storage spot specified in step 2
                String CallID = gt.GetCallID();
                String SolutionCauseData = getData("Cause", idx);
                String SolutionResolvedData = getData("Solution", idx);
                String SolutionDesc = getData("Description", idx); // TODO: NB: Might Conflict with the other element called Description! Change this name NOWWWW - in task model i think, or Web Service Itself
                sp.updateCallLog(CallID, SolutionCauseData, SolutionResolvedData, SolutionDesc);
            }           
            return "success";
        }

        [WebMethod]
        public void LogTaskTime(String totalTaskTime) // this is the last step to be done, so we can clear the working data file after this.
        {
            String username = gt.CheckCurrentUser();
            userDetails = (ArrayList)sp.getPassword(username);
            if (userDetails.Count != 0)
            { 
                //Log the time completion
                String loggindate = gt.GetLogginDate();
                String loggintime = gt.GetLogginTime();

                sp.updateUserModelDisplaySolScreenAndTimeCompleted(username, loggindate, loggintime, "YES", int.Parse(totalTaskTime), DateTime.Now.ToLongTimeString());
            }
            String _TodayDate = DateTime.Today.ToShortDateString().Replace('/', '-');
            String _TodayTime = DateTime.Now.ToLongTimeString();

            gt.SetLogginDate(_TodayDate);
            gt.SetLogginTime(_TodayTime);
        }

        [WebMethod]
        public string processAll(DataObject[] idx)
        {
            //use the other steps to write data
            //Step1 (idx)
            Step2(idx);
            Step3(idx);
            Step4(idx);
            return "success";
        }
        /// <summary>
        /// Returns the value from an object with the given name (s)
        /// </summary>
        /// <param name="s"></param>
        /// <param name="idx"></param>
        /// <returns></returns>
        public String getData(String s, DataObject[] idx)
        {
            for (int i = 0; i < idx.Length; i++)
            {
                if (idx[i].Name == s)
                    return idx[i].Value;
            }
            return "";
        }
        private String getCallID()
        {
            StoredProcedures sp = new StoredProcedures();
            String callID = (String)sp.getMAxCallID();
            if (callID == "")
            {
                callID = "1";
            }
            else
            {
                int c = Convert.ToInt32(callID);
                c += +1;
                callID = c.ToString();
            }
            return callID;
        }
    }
    [Serializable]
    public class DataObject
    {
        String _name;
        String _value;
        public DataObject()
        {
        }
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public String Value
        {
            get { return _value; }
            set { _value = value; }
        }
    }
}
